/**
 * Copyright ©2003~2019年8月13日 szkingdom
 * @author raohj
 * @date 2019年8月13日
 * @file RRSAAlgorithm.java
 * @comment : please input your comment to this file
 * @version : input version
 * @modify:
 * 		1、 2019年8月13日 由 raohj 新建文件
 */
package com.ccnu.crypt;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * @author raohj
 * @date 2019年8月13日
 * @version 
 * @comment to add your comment
 */
public class RRSA {
	public static KeyPair genKeyPair() {
		try {
			// 实例化密钥生成器		
			KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
			// 初始密钥生成器
			keyPairGen.initialize(1024);
			KeyPair keyP = keyPairGen.generateKeyPair();
			return keyP;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static byte [] decodeByPrivateKey(byte[] priKeyByte, byte [] data) {
		// 私钥解密		
		try {
			PKCS8EncodedKeySpec pkcs8Pri = new PKCS8EncodedKeySpec(priKeyByte);
			KeyFactory keyFDecode = KeyFactory.getInstance("RSA");
			PrivateKey priKey = keyFDecode.generatePrivate(pkcs8Pri);
			Cipher cipHerDecode = Cipher.getInstance(keyFDecode.getAlgorithm());
			cipHerDecode.init(Cipher.DECRYPT_MODE, priKey);
			byte [] bRSADecode = cipHerDecode.doFinal(data);
			return bRSADecode;		
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static byte [] decodeByPublicKey(byte[] pubKeyByte, byte [] data) {
		// 公钥加密
		try {
			X509EncodedKeySpec x509SpecPub = new X509EncodedKeySpec(pubKeyByte);
			KeyFactory keyF = KeyFactory.getInstance("RSA");
			PublicKey pubKey = keyF.generatePublic(x509SpecPub);
			Cipher cipHerEncode = Cipher.getInstance(keyF.getAlgorithm());
			cipHerEncode.init(Cipher.DECRYPT_MODE, pubKey);
			byte [] bRSADecode = cipHerEncode.doFinal(data);		
			return bRSADecode;
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static byte [] encodeByPrivateKey(byte[] priKeyByte, byte [] data) {
		// 私钥解密		
		try {
			PKCS8EncodedKeySpec pkcs8Pri = new PKCS8EncodedKeySpec(priKeyByte);
			KeyFactory keyFDecode = KeyFactory.getInstance("RSA");
			PrivateKey priKey = keyFDecode.generatePrivate(pkcs8Pri);
			Cipher cipHerDecode = Cipher.getInstance(keyFDecode.getAlgorithm());
			cipHerDecode.init(Cipher.ENCRYPT_MODE, priKey);
			byte [] bRSAEncode = cipHerDecode.doFinal(data);
			return bRSAEncode;	
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static byte [] encodeByPublicKey(byte[] pubKeyByte, byte [] data) {
		// 公钥加密
		try {
			X509EncodedKeySpec x509SpecPub = new X509EncodedKeySpec(pubKeyByte);
			KeyFactory keyF = KeyFactory.getInstance("RSA");
			PublicKey pubKey = keyF.generatePublic(x509SpecPub);
			Cipher cipHerEncode = Cipher.getInstance(keyF.getAlgorithm());
			cipHerEncode.init(Cipher.ENCRYPT_MODE, pubKey);
			byte [] bRSAEncode = cipHerEncode.doFinal(data);		
			return bRSAEncode;
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}
}
